﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IntillisenseLib;

namespace LittleSolution
{
    public partial class usRegisterObject : UserControl
    {
        private bool isEditTextObj = false;
        private TextObject selectedTextObj = null;

        private bool isEditContactObj = false;
        private ContactObject selectedContactObj = null;

        private PlaceholderObject selectedPlaceholderObject = null;
        public usRegisterObject()
        {
            InitializeComponent();
        }

        private void usRegisterObject_Load(object sender, EventArgs e)
        {
            //init value for all form
            InitValueforTexttab();
            InitValueforContacttab();
            InitValueforPlaceholdertab();
        }

        private void InitValueforPlaceholdertab()
        {            
        }

        private void InitValueforContacttab()
        {
            List<ObjectTemplete> objs = AppDoc.GetInstance().ObjRoot.ObjList.Where(obj => obj.ObjType == eObjectType.CONTACT).ToList();
            List<ContactObject> tobjs = new List<ContactObject>();
            foreach (ObjectTemplete obj in objs)
            {
                tobjs.Add(obj as ContactObject);
            }
            dgvListContacts.DataSource = tobjs.ToArray();
        }

        private void InitValueforTexttab()
        {
            List<ObjectTemplete> objs = AppDoc.GetInstance().ObjRoot.ObjList.Where(obj => obj.ObjType == eObjectType.TEXT).ToList();
            List<TextObject> tobjs=new List<TextObject>();
            foreach (ObjectTemplete obj in objs)
            {
                tobjs.Add(obj as TextObject);
            }
            dgvListParameter.DataSource = tobjs.ToArray();
        }

        private void btnAddText_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsValidInput())
                {
                    TextObject tobj = new TextObject(tbxParaName.Text, tbxParaValue.Text);
                    AppDoc.GetInstance().AddObj(tobj);

                    InitValueforTexttab();

                    tbxParaName.Text = "";
                    tbxParaValue.Text = "";
                    tbxParaName.Focus();
                }
                else
                {
                    MessageBox.Show("You must enter parameter name and value before add", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbxParaName.SelectAll();
                    tbxParaName.Focus();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbxParaName.SelectAll();
                tbxParaName.Focus();
            }
            
        }

        private bool IsValidInput()
        {
            return !String.IsNullOrEmpty(tbxParaName.Text) && !String.IsNullOrEmpty(tbxParaName.Text);
        }

        private void dgvListParameter_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgvListParameter.SelectedRows.Count>0)
            {
                btnEditText_Click(btnEditText, null);              
            }
        }

        private void dgvListParameter_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnDeleteText.Enabled = dgvListParameter.SelectedRows.Count > 0;
            btnEditText.Enabled = dgvListParameter.SelectedRows.Count > 0;
        }

        private void btnDeleteText_Click(object sender, EventArgs e)
        {
            AppDoc.GetInstance().DeleteObj(dgvListParameter.SelectedRows[0].DataBoundItem as TextObject);
            InitValueforTexttab();
        }

        private void dgvListParameter_DataSourceChanged(object sender, EventArgs e)
        {
            btnDeleteText.Enabled = dgvListParameter.RowCount > 0;
            btnEditText.Enabled = dgvListParameter.RowCount > 0;
        }

        private void btnAddContact_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsContactValid())
                {
                    ContactObject contact = new ContactObject(tbxFullName.Text, tbxEmail.Text, rtbxAddress.Text, tbxPhoneNumber.Text);
                    AppDoc.GetInstance().AddObj(contact);

                    InitValueforContacttab();

                    tbxFullName.Text = "";
                    tbxEmail.Text = "";
                    rtbxAddress.Text = "";
                    tbxPhoneNumber.Text = "";
                    tbxFullName.Focus();
                }
                else
                {
                    MessageBox.Show("You must enter full name before add", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tbxFullName.SelectAll();
                    tbxFullName.Focus();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                tbxFullName.SelectAll();
                tbxFullName.Focus();
            }
        }
        private bool IsContactValid()
        {
            return !String.IsNullOrEmpty(tbxFullName.Text);
        }

        private void dgvListContacts_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnDeleteContact.Enabled = dgvListContacts.SelectedRows.Count > 0;
            btnEditContact.Enabled = dgvListContacts.SelectedRows.Count > 0;
        }

        private void dgvListContacts_DataSourceChanged(object sender, EventArgs e)
        {
            btnDeleteContact.Enabled = dgvListContacts.RowCount > 0;
            btnEditContact.Enabled = dgvListContacts.RowCount > 0;
        }

        private void btnDeleteContact_Click(object sender, EventArgs e)
        {
            AppDoc.GetInstance().DeleteObj(dgvListContacts.SelectedRows[0].DataBoundItem as ContactObject);
            InitValueforContacttab();
        }


        private void btnEditText_Click(object sender, EventArgs e)
        {

            isEditTextObj = !isEditTextObj;

            if (isEditTextObj)
            {
                selectedTextObj = dgvListParameter.SelectedRows[0].DataBoundItem as TextObject;
                tbxParaName.Text = selectedTextObj.ParameterName;
                tbxParaValue.Text = selectedTextObj.ParamaterValue;

                btnEditText.Text = "Update";

                AppDoc.GetInstance().DeleteObj(selectedTextObj);
            }
            else
            {
                selectedTextObj.ParameterName = tbxParaName.Text;
                selectedTextObj.ParamaterValue = tbxParaValue.Text;

                try
                {
                    AppDoc.GetInstance().AddObj(selectedTextObj);

                    btnEditText.Text = "Edit";

                    InitValueforTexttab();

                    selectedTextObj = null;
                    tbxParaName.Text = "";
                    tbxParaValue.Text = "";
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    isEditTextObj = !isEditTextObj;
                }
                
            }

            btnAddText.Enabled = !isEditTextObj;
            btnDeleteText.Enabled = !isEditTextObj;
            dgvListParameter.Enabled = !isEditTextObj;

            tbxParaName.SelectAll();
            tbxParaName.Focus();
        }

        private void btnEditContact_Click(object sender, EventArgs e)
        {
            isEditContactObj = !isEditContactObj;

            if (isEditContactObj)
            {
                selectedContactObj = dgvListContacts.SelectedRows[0].DataBoundItem as ContactObject;
                tbxFullName.Text = selectedContactObj.FullName;
                tbxEmail.Text = selectedContactObj.Email;

                tbxPhoneNumber.Text = selectedContactObj.PhoneNumber;
                rtbxAddress.Text = selectedContactObj.Address;

                btnEditContact.Text = "Update";

                AppDoc.GetInstance().DeleteObj(selectedContactObj);
            }
            else
            {
                selectedContactObj.FullName = tbxFullName.Text;
                selectedContactObj.Email = tbxEmail.Text;
                selectedContactObj.PhoneNumber = tbxPhoneNumber.Text;
                selectedContactObj.Address = rtbxAddress.Text;

                try
                {
                    AppDoc.GetInstance().AddObj(selectedContactObj);

                    btnEditContact.Text = "Edit";

                    InitValueforContacttab();

                    selectedContactObj = null;
                    tbxFullName.Text = "";
                    tbxEmail.Text = "";

                    tbxPhoneNumber.Text = "";
                    rtbxAddress.Text = "";
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    isEditContactObj = !isEditContactObj;
                }

            }

            btnAddContact.Enabled = !isEditContactObj;
            btnDeleteContact.Enabled = !isEditContactObj;
            dgvListContacts.Enabled = !isEditContactObj;

            tbxFullName.SelectAll();
            tbxFullName.Focus();
        }

        private void dgvListContacts_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgvListContacts.SelectedRows.Count > 0)
            {
                btnEditContact_Click(btnEditContact, null);
            }
        }

        private void addNewObjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddObject addObjectDlg = new frmAddObject();
            if (addObjectDlg.ShowDialog()==DialogResult.OK)
            {
                PlaceholderObject obj = new PlaceholderObject(addObjectDlg.GetObjectName(), addObjectDlg.GetObjectName());

                try
                {
                    AppDoc.GetInstance().AddObj(obj);

                    TreeNode node = new TreeNode();
                    node.Text = addObjectDlg.GetObjectName();
                    node.Tag = addObjectDlg.GetObjectName();
                    node.ContextMenuStrip = cmsClass;

                    treeView1.Nodes[0].Nodes.Add(node);
                    
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                 	
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            PlaceholderObject rootObject = GetRootObject(e.Node);
            if (rootObject!=null)
            {
                if (rootObject.ObjName==e.Node.Text)
                {
                    selectedPlaceholderObject = rootObject;
                }
                else
                {
                    selectedPlaceholderObject = rootObject.FindProperties(e.Node.Text);
                }                
            }                
        }

        private void addPropertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddProperties addPropertiesDlg = new frmAddProperties();
            if (addPropertiesDlg.ShowDialog()==DialogResult.OK)
            {
                if (selectedPlaceholderObject!=null)
                {
                    PlaceholderObject newObj = new PlaceholderObject(addPropertiesDlg.GetPropertiesName(), addPropertiesDlg.GetPropertiesValue());
                    selectedPlaceholderObject.AddChild(newObj);

                    TreeNode newNode=new TreeNode();
                    newNode.Text=addPropertiesDlg.GetPropertiesName();
                    newNode.Tag=addPropertiesDlg.GetPropertiesName();
                    newNode.ContextMenuStrip = cmsClass;
                    newNode.ToolTipText = "Value: " + addPropertiesDlg.GetPropertiesValue();

                    treeView1.SelectedNode.Nodes.Add(newNode);
                }
            }
        }
        private PlaceholderObject GetRootObject(TreeNode node)
        {            
            if (node.Parent==null)
            {
                return null;
            }
            else
            {
                while (node.Parent.Text!="Object Browser")
                {
                    node = node.Parent;
                }

                String text = node.Text;
                return AppDoc.GetInstance().FindObject(text, eObjectType.PLACEHOLDER) as PlaceholderObject;
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAddProperties addPropertiesDlg = new frmAddProperties();

            addPropertiesDlg.SetValue(selectedPlaceholderObject.ObjName, selectedPlaceholderObject.ParaValue);
            if (addPropertiesDlg.ShowDialog() == DialogResult.OK)
            {
                selectedPlaceholderObject.ObjName = addPropertiesDlg.GetPropertiesName();
                selectedPlaceholderObject.ParaValue = addPropertiesDlg.GetPropertiesValue();

                treeView1.SelectedNode.Text = addPropertiesDlg.GetPropertiesName();
                treeView1.SelectedNode.Tag = addPropertiesDlg.GetPropertiesValue();
                treeView1.SelectedNode.ToolTipText = "Value: " + addPropertiesDlg.GetPropertiesValue();
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you want delete this object?", "Question", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
              {
                  PlaceholderObject parentObject = (PlaceholderObject)selectedPlaceholderObject.Parent;

                  if (parentObject == null)
                  {
                      AppDoc.GetInstance().DeleteObj(selectedPlaceholderObject);
                  }
                  else
                  {
                      parentObject.RemoveChild(selectedPlaceholderObject);
                  }

                  treeView1.SelectedNode.Remove();
              }
              
        }

        private void deleteAllObjectsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AppDoc.GetInstance().DeleteAllObject(eObjectType.PLACEHOLDER);
            treeView1.SelectedNode.Nodes.Clear();                                     
        }
    }
}
